package cn.microven.mindmap.controller.menu;

import cn.microven.mindmap.components.part.BothSideMapPart;
import cn.microven.mindmap.components.part.LeftMapPart;
import cn.microven.mindmap.components.part.Part;
import cn.microven.mindmap.components.part.RightMapPart;
import cn.microven.mindmap.oprations.FileOperation;
import cn.microven.mindmap.struture.MindMap;
import cn.microven.mindmap.utils.ComponentGetter;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.*;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class FileMenu implements Initializable
{
    @FXML
    public AnchorPane pictureSave;

    @FXML
    public AnchorPane fileSave;

    @FXML
    public AnchorPane fileOpen;

    @FXML
    public AnchorPane saveAsOther;

    private File openedFile;//当前保存的思维导图目标文件，下次保存直接覆盖该文件

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        pictureSave.setOnMouseClicked((mouseEvent -> {
            FileChooser fc = new FileChooser();
            fc.setTitle("保存图片文件");
            fc.setInitialDirectory(new File("."));
            fc.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.JPG"),
                    new FileChooser.ExtensionFilter("图片类型", "*.png", "*.PNG")
            );
            File file = fc.showSaveDialog(new Stage());
            if(file != null) {
                //将思维导图界面截图保存到文件中
                MindMap.getInstance().clearSelect();
                WritableImage image = MindMap.getInstance().getRoot().snapshot(new SnapshotParameters(), null);
                try {
                    ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }));

        fileSave.setOnMouseClicked((mouseEvent -> {
            if(openedFile == null) {
                FileChooser fc = new FileChooser();
                fc.setTitle("保存文件");
                fc.setInitialDirectory(new File("."));
                fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("思维导图类型", "*.mpp", "*.MPP"));
                openedFile = fc.showSaveDialog(new Stage());
            }
            if(openedFile != null) {
                try {
                    //保存思维导图文件
                    FileOperation.writeFile(MindMap.getMap().getRoot(), openedFile.getPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }));

        fileOpen.setOnMouseClicked((mouseEvent -> {
            FileChooser fc = new FileChooser();
            fc.setTitle("读取文件");
            fc.setInitialDirectory(new File("."));
            //读取文件只能读取思维导图类型的文件
            fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("思维导图类型", "*.mpp", "*.MPP"));
            File file = fc.showOpenDialog(new Stage());
            if(file != null) {
                openedFile = file;
                try {
                    BothSideMapPart newmap = FileOperation.readFile(file.getPath());
                    MindMap.getInstance().setRoot(newmap);
//                    System.out.println(MindMap.getInstance().getChildren());
                    TreeItem<String> rt = MindMap.getInstance().getRoot().getTreeItem();
                    ComponentGetter.getTreeView().setRoot(rt);

                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }));

        saveAsOther.setOnMouseClicked((mouseEvent -> {
            save();
        }));
    }

    public void save(){
        FileChooser fc = new FileChooser();
        fc.setTitle("文件另存为");
        fc.setInitialDirectory(new File("."));
        fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("思维导图类型", "*.mpp", "*.MPP"));
        File file = fc.showSaveDialog(new Stage());
        if(file != null) {
            try {
                //保存思维导图文件
                FileOperation.writeFile(MindMap.getMap().getRoot(), file.getPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
